/*  

Cafechess Library: Cover basic chess programming needs, like GUI, Engine integration, 
FEN and PGN parsing.

                 \\|//
                 (o o)
--------------ooO-(_)-Ooo----------------------------------
Copyright (C) 2009  By Gregory A. Prentice Snoqualmie, WA
                    www.cafechess.org
                    cafechess@comcast.net
                      Oooo
--------------oooO----(  )---------------------------------
              (  )    (_/
               \_)

Microsoft Public License (Ms-PL)
               
Revisions:

Author        Date    Description:

*/
using System;
using System.Text.RegularExpressions;
namespace Cafechess.Text.Commands
{
	/// <summary>
	/// Used to associate a regular expression that will
	/// match on a command and it's arguments.  If a match
	/// is found then the associated ComsumeEventHandler
	/// is called to process the arguments.
	/// </summary>
  public class Command : Regex
  {
    /// <summary>
    /// If true then this command should also have an argument associated
    /// with it.
    /// </summary>
    public bool IsCommand
    {
      get{ return coIsCommand; }
      set{ coIsCommand = value; }
    }
    bool coIsCommand;

    ChessEventArgs coArgs = new ChessEventArgs("");
    
    /// <summary>
    /// Definition of the event handler that will get called to handle this command.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void ConsumerEventHandler(object sender, System.EventArgs e);
    
    /// <summary>
    /// Event handler to call if it is assigned.
    /// </summary>
    private event ConsumerEventHandler Consumer;
    /// <summary>
    /// Constructor that initializes our Command object to make it aware of the
    /// string signature that triggers an execution of the ConsumerEventHandler.
    /// </summary>
    /// <param name="expression">A regular expression that is used to recognise a string signiture for command recognition</param>
    /// <param name="options">Regular Expression options</param>
    /// <param name="isCommand">If true then the command also contains parameters</param>
    /// <param name="cmd">The ConsumerEventHandler to execute when a match is found.</param>
    public Command(string expression, RegexOptions options, bool isCommand, ConsumerEventHandler cmd) :base(expression,options)
    {
      coIsCommand = isCommand;
      Consumer = cmd;
    }
    /// <summary>
    /// This routine will simply check for a match and if one is
    /// found, call the ConsumerEventHandler.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="command"></param>
    /// <returns></returns>
    public bool Process(object sender, string command)
    {
      Match m;
      m = Match(command);
      if(m.Success)
      {
        // If a command then we expect the last item in the group contains
        //  the parameters for the command and are extracted and passed, else 
        //  the original string is passed in it's entirety to the handler.
        if(IsCommand)
        {
          System.Text.RegularExpressions.Group grp = m.Groups[m.Groups.Count-1];
          command = grp.ToString(); 
        }
        if(Consumer != null)
        {
          coArgs.Information = command;
          Consumer(sender,coArgs);
        }
      }
      return m.Success;
    }
  }
  /// <summary>
  /// Used to pass arguments around to the event handlers.
  /// </summary>
  public class ChessEventArgs: EventArgs 
  {
    public ChessEventArgs(string data) 
    {
      Information = data;
    }

    public string Information;    

  }


}
